<template>
  <!-- 添加或修改车辆信息对话框 -->
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="1000px"
    append-to-body
    :close-on-click-modal="false"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="98px" label-position="right" class="flex-form"
             size="small">
      <el-form-item label="车牌号" prop="plateNumber">
        <el-input v-model.trim="form.plateNumber" placeholder="请输入车牌号"/>
      </el-form-item>

      <el-form-item label="车牌颜色" prop="plateColorName">
        <el-input v-model.trim="form.plateColorName" placeholder="请输入车牌颜色"/>
      </el-form-item>

      <el-form-item label="能源类型" prop="energyTypeName">
        <el-select v-model.trim="form.energyTypeName" placeholder="请选择能源类型">
          <el-option
            v-for="dict in dict.type.energy_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="价格" prop="vin">
        <el-input v-model.trim="form.vin" placeholder="请输入价格"/>
      </el-form-item>

      <el-form-item label="购车日期" prop="purchaseDate">
        <el-date-picker clearable size="small"
                        v-model.trim="form.purchaseDate"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择购车日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="发动机号" prop="engineNum">
        <el-input v-model.trim="form.engineNum" placeholder="请输入发动机号"/>
      </el-form-item>

      <el-form-item label="年审日期" prop="annualReviewTime">
        <el-date-picker clearable size="small"
                        v-model.trim="form.annualReviewTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择年审日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="车辆道路运输证号" prop="vehicleTransportationLicenseNum">
        <el-input v-model.trim="form.vehicleTransportationLicenseNum" placeholder="请输入车辆道路运输证号"/>
      </el-form-item>

      <el-form-item label="车辆道路运输证">
        <fileUpload v-model.trim="form.vehicleTransportationLicenseUrl"/>
      </el-form-item>

      <el-form-item label="车辆道路运输证到期时间" prop="vehicleTransportationLicenseEndTime">
        <el-date-picker clearable size="small"
                        v-model.trim="form.vehicleTransportationLicenseEndTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择车辆道路运输证到期时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="强制报废时间" prop="compulsoryScrapTime">
        <el-date-picker clearable size="small"
                        v-model.trim="form.compulsoryScrapTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择强制报废时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="行驶证照片正面">
        <imageUpload v-model.trim="form.vehicleLicenseHomeUrl"/>
      </el-form-item>

      <el-form-item label="行驶证照片反面">
        <imageUpload v-model.trim="form.vehicleLicenseViceUrl"/>
      </el-form-item>

      <el-form-item label="行驶证号" prop="vehicleLicenseNum">
        <el-input v-model.trim="form.vehicleLicenseNum" placeholder="请输入行驶证号"/>
      </el-form-item>

      <el-form-item label="车辆照片">
        <imageUpload v-model.trim="form.vehiclePhotoUrl"/>
      </el-form-item>

      <el-form-item label="机动车登记证">
        <fileUpload v-model.trim="form.vehicleRegistrationCertificateUrl"/>
      </el-form-item>

      <el-form-item label="etc卡号" prop="etcAccountNum">
        <el-input v-model.trim="form.etcAccountNum" placeholder="请输入etc卡号"/>
      </el-form-item>

      <el-form-item label="车辆类型" prop="vehicleType">
        <el-select v-model.trim="form.vehicleType" placeholder="请选择车辆类型">
          <el-option
            v-for="dict in dict.type.yj_vehicle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="行驶证到期日" prop="vehicleLicenseEndTime">
        <el-date-picker clearable size="small"
                        v-model.trim="form.vehicleLicenseEndTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择行驶证到期日">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="交强险到期日" prop="compulsoryTrafficInsurance">
        <el-date-picker clearable size="small"
                        v-model.trim="form.compulsoryTrafficInsurance"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择交强险到期日">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="商业险到期日" prop="commercialInsurance">
        <el-date-picker clearable size="small"
                        v-model.trim="form.commercialInsurance"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择商业险到期日">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="关联企业" prop="enterpriseId">
        <el-input v-model.trim="form.enterpriseId" placeholder="请输入关联企业"/>
      </el-form-item>

      <el-form-item label="同步状态(0-未发送，1-已发送)" prop="syncStatus">
        <el-select v-model.trim="form.syncStatus" placeholder="请选择同步状态(0-未发送，1-已发送)">
          <el-option
            v-for="dict in dict.type.sync_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="同步人" prop="syncBy">
        <el-input v-model.trim="form.syncBy" placeholder="请输入同步人"/>
      </el-form-item>

      <el-form-item label="同步时间" prop="syncTime">
        <el-date-picker clearable size="small"
                        v-model.trim="form.syncTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择同步时间">
        </el-date-picker>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getYjVehicleInfo, addYjVehicleInfo, updateYjVehicleInfo} from "@/api/miniapp/yjVehicleInfo";

export default {
  name: "YjVehicleInfo-addEdit",
  props: {
    dialogType: {
      type: String,
      default: "add",
    },
    rowData: {
      type: Object,
      default: () => {
      },
    },
    dict: {}
  },
  data() {
    return {
      // 弹出层
      dialogVisible: false,
      // 标题
      title: "新增",
      // 表单参数
      form: {
        id: null,
        plateNumber: null,
        plateColorName: null,
        energyTypeName: null,
        vin: null,
        purchaseDate: null,
        engineNum: null,
        annualReviewTime: null,
        vehicleTransportationLicenseNum: null,
        vehicleTransportationLicenseUrl: null,
        vehicleTransportationLicenseEndTime: null,
        compulsoryScrapTime: null,
        vehicleLicenseHomeUrl: null,
        vehicleLicenseViceUrl: null,
        vehicleLicenseNum: null,
        vehiclePhotoUrl: null,
        vehicleRegistrationCertificateUrl: null,
        etcAccountNum: null,
        vehicleType: null,
        vehicleLicenseEndTime: null,
        compulsoryTrafficInsurance: null,
        commercialInsurance: null,
        enterpriseId: null,
        syncStatus: null,
        syncBy: null,
        syncTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null

      },
      // 车辆信息表格数据
      yjVehicleInfoList: [],
      // 表单校验
      rules: {
        plateNumber: [
          {required: true, message: "车牌号不能为空", trigger: "blur"}
        ],
        plateColorName: [
          {required: true, message: "车牌颜色不能为空", trigger: "blur"}
        ],
      }
    };
  },
  methods: {
    open() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        if (this.dialogType == "add") {
          this.title = "新增";
        } else if (this.dialogType == "edit") {
          this.title = "编辑";
          this.loadDetail();
        } else if (this.dialogType == "detail") {
          this.title = "详情";
          this.loadDetail();
        }
      });
    },
    close() {
      this.$refs["form"].resetFields();
      this.dialogVisible = false;
    },

    // 获取详情
    loadDetail() {
      const id = this.rowData.id;
      getYjVehicleInfo(id).then((res) => {
        if (res.code == 200) {
          this.form = res.data;
        }
      });
    },

    // 提交表单
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = {
            ...this.form,
          };
          if (this.dialogType == "add") {
            addYjVehicleInfo(this.form).then(res => {
              if (res.code === 200) {
                this.$modal.msgSuccess("新增成功");
                this.close();
                this.$emit("handleQuery");
              }
            });
          } else {
            params.id = this.rowData.id;
            updateYjVehicleInfo(this.form).then(res => {
              if (res.code === 200) {
                this.$modal.msgSuccess("修改成功");
                this.close();
                this.$emit("handleQuery");
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
